<%@LANGUAGE="VBSCRIPT" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Address Book</title>
<link href="UniversalStyleSheet.css" rel="stylesheet" type="text/css" />
<%
action=request.QueryString("action")
select case action
	case "addcontact"
		adddisplay=request.QueryString("adddisplay")
		addemail=request.QueryString("addemail")
		set fs=server.CreateObject("scripting.filesystemobject")
		set cFile=fs.opentextfile(server.MapPath("access/backend/contacts.txt"),8,true)
		cFile.writeline (adddisplay & "|" & addemail)
		cFile.close
		set cFile=nothing
		set fs=nothing
		response.Redirect("addressbook.asp?sendback=" & request.QueryString("sendback") & "&val=" & request.QueryString("val"))
		
	case "changecontact"
		changedisplay=request.QueryString("adddisplay")
		changeemail=request.QueryString("addemail")
		set fs=server.CreateObject("scripting.filesystemobject")
		set rs=server.CreateObject("adodb.recordset")
		set cFile=fs.opentextfile(server.MapPath("access/backend/contacts.txt"),1,true)
		with rs.fields
			.append "display",202,100 
			.append "email",202,100 
		end with
		rs.open()
		do until cFile.atendofstream
			rs.addnew
			rl=cFile.readline
			record=split(rl,"|")
			rs("display")=record(0)
			if ubound(record)>0 then
				rs("email")=record(1)
			else
				rs("email")=""
			end if
			rs.update
		loop
		cFile.close
		set cFile=fs.opentextfile(server.MapPath("access/backend/contacts.txt"),2,true)
		if not rs.eof then
			rs.movefirst
			do until rs.eof
				if (rs("display")=changedisplay) or (rs("email")=changeemail) then
					cFile.writeline (changedisplay & "|" & changeemail)
				else
					cFile.writeline (rs("display") & "|" & rs("email"))
				end if
				rs.movenext
			loop
			rs.close
			cFile.close
			set rs=nothing
			set fs=nothing
			response.Redirect("addressbook.asp?sendback=" & request.QueryString("sendback") & "&val=" & request.QueryString("val"))
		end if
	
	case "deletecontact"
		changedisplay=request.QueryString("adddisplay")
		changeemail=request.QueryString("addemail")
		set fs=server.CreateObject("scripting.filesystemobject")
		set rs=server.CreateObject("adodb.recordset")
		set cFile=fs.opentextfile(server.MapPath("access/backend/contacts.txt"),1,true)
		with rs.fields
			.append "display",202,100 
			.append "email",202,100 
		end with
		rs.open()
		do until cFile.atendofstream
			rs.addnew
			rl=cFile.readline
			record=split(rl,"|")
			rs("display")=record(0)
			rs("email")=record(1)
			rs.update
		loop
		cFile.close
		set cFile=fs.opentextfile(server.MapPath("access/backend/contacts.txt"),2,true)
		if not rs.eof then
			rs.movefirst
			do until rs.eof
				if (rs("display")<>changedisplay) and (rs("email")<>changeemail) then
					cFile.writeline (rs("display") & "|" & rs("email"))
				end if
				rs.movenext
			loop
			rs.close
			cFile.close
			set rs=nothing
			set fs=nothing
			response.Redirect("addressbook.asp?sendback=" & request.QueryString("sendback") & "&val=" & request.QueryString("val"))
		end if
end select
%>
<script type="text/javascript">
function ClearContact_click()
{
	ad=document.getElementById("AddDisplay")
	ae=document.getElementById("AddEmail")
	ad.value=""
	ae.value=""
}

function Clrbtn(){
	document.getElementById("List").value=""
}

function OKbtn(){
	sendback="<%= request.QueryString("sendback") %>"
	window.opener.document.getElementById(sendback).value=document.getElementById("List").value
	window.close()
}	

function DeleteContact_click()
{
	ad=document.getElementById("AddDisplay")
	ae=document.getElementById("AddEmail")
	l=document.getElementById("List")
	if (ad.value !="" || ae.value !="")
		{
		window.location.href="addressbook.asp?action=deletecontact&adddisplay=" + ad.value + "&addemail=" + ae.value + "&sendback=<%=request.QueryString("sendback") %>&val=" + l.value
		}
	else
		{
		alert ("Both 'Display Name' and 'Email Address' must have something in them  in order to delete a contact.")
		}
}

function ChangeContact_click()
{
	ad=document.getElementById("AddDisplay")
	ae=document.getElementById("AddEmail")
	l=document.getElementById("List")

	if (ad.value !="" || ae.value !="")
		{
		window.location.href="addressbook.asp?action=changecontact&adddisplay=" + ad.value + "&addemail=" + ae.value + "&sendback=<%=request.QueryString("sendback") %>&val=" + l.value
		}
	else
		{
		alert ("Both 'Display Name' and 'Email Address' must have something in them  in order to change a contact.")
		}
}

function AddContact_click()
{
	ad=document.getElementById("AddDisplay")
	ae=document.getElementById("AddEmail")
	l=document.getElementById("List")
	if (ad.value !="" || ae.value !="")
		{
		window.location.href="addressbook.asp?action=addcontact&adddisplay=" + ad.value + "&addemail=" + ae.value + "&sendback=<%=request.QueryString("sendback") %>&val=" + l.value
		}
	else
		{
		alert ("Both 'Display Name' and 'Email Address' must be completed in order to add a new contact.")
		}
}

function BtnAdd_Click()
{
	c=document.getElementById("Contacts")
	l=document.getElementById("List")
	lv=l.value
	if (c.selectedIndex != -1)
		{
		if (lv=="")
			{
			lv=c.options[c.selectedIndex].text + " <" + c.options[c.selectedIndex].id + ">"
			}
		else
			{
			lv=lv + ";" + c.options[c.selectedIndex].text + " <" + c.options[c.selectedIndex].id + ">"
			}
		}
	l.value=lv
	
}

function Contacts_change(){
	d=document.getElementById("AddDisplay")
	e=document.getElementById("AddEmail")
	c=document.getElementById("Contacts")
	d.value=c.options[c.selectedIndex].value
	e.value=c.options[c.selectedIndex].id
}
</script>
</head>

<body>
<table width="550" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="20" height="20" background="Images/TableUpperLeft.png"></td>
    <td width="510" height="20" background="Images/TableUpperMiddle.png"></td>
    <td width="20" height="20" background="Images/TableUpperRight.png"></td>
  </tr>
  <tr>
    <td width="20" background="Images/TableMiddleLeft.png"></td>
    <td bgcolor="#FFFFFF"><form id="AddrBookForm" name="AddrBookForm" method="post" action="">
      <table width="510" border="0" cellpadding="0" cellspacing=" 1" bgcolor="#009900">
        <tr>
          <td width="51" align="left" valign="top" bgcolor="#FFFFFF" class="ResultsAddress">Contacts</td>
          <td width="142" align="left" valign="top" bgcolor="#FFFFFF" class="ResultsAddress">
		  	<select name="Contacts" size="10" id="Contacts" onchange="Contacts_change()" >
		  	<%
			set fs=server.CreateObject("scripting.filesystemobject")
			set cFile=fs.opentextfile(server.MapPath("access/backend/contacts.txt"),1,true)
			do while not cFile.atendofstream
				row=cFile.readline
				contact=split(row,"|")
				display=contact(0)
				email=contact(1)
				%>
				<option value="<%= display %>" id="<%= email %>"><%= display %></option> 
				<%
			loop
			%>
          	</select>
			</td>
          <td width="41" align="center" valign="middle" bgcolor="#FFFFFF" class="ResultsAddress"><input name="BtnAdd" type="button" class="buttons" id="BtnAdd" title="Clicking Here Will Add The Contact Chosen On The Left To The Recipient List On The Right" onclick="javascript: BtnAdd_Click()" value="  &gt;  " /></td>
          <td width="221" align="left" valign="middle" bgcolor="#FFFFFF" class="ResultsName">
		  <%= mid(request.QueryString("sendback"),2) %>:<br />
		  <textarea name="List" cols="30" rows="8" id="List"><%= request.QueryString("val") %></textarea></td>
        </tr>
        <tr>
          <td colspan="4" align="right" valign="top" bgcolor="#FFFFFF" class="ResultsAddress"><img src="Images/ClearButton.png" alt="clear" width="60" height="30" border="0" title="Clicking 'Clear' Will Clear The Contents Of The Recipient Box Above" onclick="vbscript: Clrbtn()"/> <img src="Images/OKButton.png" alt="OK" width="60" height="30" border="0" title="Clicking 'OK' Will Insert The Recipients Listed In The Recipient Box Above Into It's Corresponding Field On The Email Form" onclick="vbscript: OKbtn()"/></td>
          </tr>
        <tr>
          <td colspan="4" align="right" valign="top" bgcolor="#D2FFD2" class="ResultsAddress"></td>
        </tr>
        <tr>
          <td colspan="3" align="left" valign="top" bgcolor="#FFFFFF" class="ResultsAddress">Add / Change A Contact:<br />
            <br />
            Display Name<br />
            <input name="AddDisplay" type="text" id="AddDisplay" size="25" />
            <br /> <br />
            Email Address<br />
            <input name="AddEmail" type="text" id="AddEmail" size="25" />
			</td>
          <td align="left" valign="bottom" bgcolor="#FFFFFF" class="ResultsAddress">
		  <input name="BtnAdd" type="button" class="buttons" id="BtnAdd" title="Clicking 'Add' Will Add What Is Typed Into The 'Display Name' and 'Email Address' Fields To The Left As A New Contact Into The Contacts List Above" onclick="AddContact_click()" value=" Add "/>
            <input name="BtnChange" type="button" class="buttons" id="BtnChange" title="Clicking 'Change' Will Either Save Changes Made To The 'Display Name' On The Left That Is Associated With The Email Address Listed To The Left Or It Will Save Changes Made To The 'Email Address' To The Left That Is Associated With The Display Name Listed To The Left.  So If You Have To Make Changes To Both The Display Name AND The Email Address Of The Contact Listed To The Left, Change One Field ('Display Name' or 'Email Address') At A Time Or Else The Changes Will Not Save At All" onclick="ChangeContact_click()" value="Change"/>
            <input name="BtnDelete" type="button" class="buttons" id="BtnDelete" title="Clicking 'Delete' Will Remove The Contact Listed To The Left Completely From The List Of Contacts" onclick="DeleteContact_click()" value="Delete"/>
            <input name="BtnClear" type="button" class="buttons" id="BtnClear" title="Clicking 'Clear' Will Clear The 'Display Name' and 'Email Address' Fields To The Left" onclick="ClearContact_click()" value="Clear"/></td>
        </tr>
      </table>
    </form>
    </td>
    <td width="20" background="Images/TableMiddleRight.png"></td>
  </tr>
  <tr>
    <td width="20" height="20" background="Images/TableLowerLeft.png"></td>
    <td width="460" height="20" background="Images/TableLowerMiddle.png"></td>
    <td width="20" height="20" background="Images/TableLowerRight.png"></td>
  </tr>
</table>

</body>
</html>
